package com.base.common.provider;

import java.util.Calendar;

import javax.faces.model.SelectItem;

/**
 * 提供一些能用的selectitem列表，通用选项，年度，月份等
 * 
 * 
 */
public class SelectItemsProvider {
	/**
	 * 取年度选择，默认为当前年份的前后五年
	 * 
	 * @param beginYear
	 *            开始年份，如果为null，是当前年的前level年
	 * @param endYear
	 *            结束年份，如果为null，是当前年的后level年
	 * @param level
	 *            相对于当前年的间隔
	 * @return
	 */
	public static SelectItem[] getYear(String beginYear, String endYear,
			int level) {
		if (level == 0)
			level = 5;
		int cYear = Calendar.getInstance().get(Calendar.YEAR);
		int sYear = cYear - level - 1;
		int eYear = cYear + level;
		if (beginYear != null && !"".equals(beginYear))
			sYear = Integer.parseInt(beginYear);
		if (endYear != null && !"".equals(endYear))
			eYear = Integer.parseInt(endYear);
		SelectItem[] result = new SelectItem[eYear - sYear + 1];
		for (int i = 0; sYear <= eYear; i++, sYear++) {
			result[i] = new SelectItem(String.valueOf(sYear), String
					.valueOf(sYear));
		}
		return result;
	}

	/**
	 * 得到所有的月份
	 * 
	 * @return
	 */
	public static SelectItem[] getAllMonth() {
		SelectItem[] result = new SelectItem[12];
		for (int i = 0; i < 12; i++) {
			result[i] = new SelectItem(new Integer(i + 1).toString(),
					new Integer(i + 1).toString());
		}
		return result;
	}

	/**
	 * 得到所有的月份，下拉选项中带 “全部”，值为0
	 * 
	 * @return
	 */
	public static SelectItem[] getAllMonthWithTotal() {
		SelectItem[] result = new SelectItem[13];
		for (int i = 0; i < 13; i++) {
			if (i == 0)
				result[0] = new SelectItem(new Integer(0).toString(), "全部");
			else
				result[i] = new SelectItem(new Integer(i).toString(),
						new Integer(i).toString());
		}
		return result;
	}

	/**
	 * 取是否的选择
	 * 
	 * @return
	 */
	public static SelectItem[] getYesNo() {
		return new SelectItem[] { new SelectItem(true, "是"),
				new SelectItem(false, "否") };
	}

	/**
	 * 取有无的选择
	 * 
	 * @return
	 */
	public static SelectItem[] getHaveNot() {
		return new SelectItem[] { new SelectItem(true, "有"),
				new SelectItem(false, "无") };
	}

	/**
	 * 取数字序列
	 * 
	 * @param last
	 * @return
	 */
	public static SelectItem[] getIndexNumber(int last) {
		if (last <= 0)
			return null;
		SelectItem[] result = new SelectItem[last];
		for (int i = 1; i <= last; i++) {
			result[i - 1] = new SelectItem(new Integer(i), String.valueOf(i));
		}
		return result;
	}
}
